package cn.songhaiqing.walle.service;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.LocalBroadcastManager;

import cn.songhaiqing.walle.Event.AboutEvent;
import cn.songhaiqing.walle.Event.MqttEvent;
import cn.songhaiqing.walle.Event.MqttTopicEvent;
import cn.songhaiqing.walle.Event.StepUpdateEvent;
import cn.songhaiqing.walle.Event.XYZEvent;
import cn.songhaiqing.walle.bean.MyApp;
import cn.songhaiqing.walle.ble_controller.BleConnectController;

import org.eclipse.paho.android.service.MqttAndroidClient;
import org.eclipse.paho.client.mqttv3.IMqttActionListener;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.IMqttToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttCallbackExtended;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import java.util.Calendar;
import java.util.List;

import cn.songhaiqing.walle.Event.BloodEvent;
import cn.songhaiqing.walle.Event.DeviceButtonPushEvent;
import cn.songhaiqing.walle.Event.DeviceConnectEvent;
import cn.songhaiqing.walle.Event.DeviceScreenEvent;
import cn.songhaiqing.walle.Event.DeviceSyncEvent;
import cn.songhaiqing.walle.Event.HeartStatusEvent;
import cn.songhaiqing.walle.Event.RRiReceiveEvent;
import cn.songhaiqing.walle.bean.HistoryHour;
import cn.songhaiqing.walle.ble_controller.BleConnectCallBack;
import cn.songhaiqing.walle.ble_controller.NoConnectException;
import cn.songhaiqing.walle.bledemo.MainActivity;
import cn.songhaiqing.walle.bledemo.R;
import cn.songhaiqing.walle.global.Global;
import cn.songhaiqing.walle.help.CommandHelper;
import cn.songhaiqing.walle.help.DeviceHelper;
import cn.songhaiqing.walle.help.FormatHelper;
import cn.songhaiqing.walle.help.ParserHelper;
import cn.songhaiqing.walle.help.SpHelper;
import cn.songhaiqing.walle.help.VersionHelper;
import cn.songhaiqing.walle.synccontroller.SyncCompat;

public class BLEService2 extends Service {
    public static final String KEYCURTime = "curTime";
    public static final String KEYHRV = "hrv";
    public static final String KEYHR_COUNT = "hr_count";
    public static final String KEYHR_T = "bt";
    public static final String KEYHR_t = "lt";
    public static final int SENDID = 2;
    public static final int STARTID = 3;
    public static final String SubscribeTopic = "bh-00/hrv/";
    private final int NOTIFICATION_ID = 1;
    private final String PublishTopic = "bh-00/heartrate/ch1";
    private String TAG = "BLEService2";
    private final int canDisConnectedCount = 5;
    private MqttAndroidClient client;

    private BleConnectCallBack connectCallBack = new BleConnectCallBack() {
        /* class com.gzgamut.smart_movement.service.BLEService2.AnonymousClass6 */

        @Override // com.gzgamut.smart_movement.ble_controller.BleConnectCallBack
        public void onConnectionStateChange(int state, String mac) {
            switch (state) {
                case 0:
                    try {
                        BLEService2.this.findGattService();
                        return;
                    } catch (NoConnectException e) {
                        e.printStackTrace();
                        return;
                    }
                case 1:
                    BLEService2.this.connectState = 1;
                    EventBus.getDefault().post(new DeviceConnectEvent(1, mac));
                    return;
                case 2:
                    BLEService2.this.connectState = 2;
                    if (BLEService2.this.syncCompat != null) {
                        BLEService2.this.syncCompat.setIsSyncing(false);
                    }
                    BLEService2.this.setIsHRVTest(false);
                    BLEService2.this.syncCompat = null;
                    EventBus.getDefault().post(new DeviceConnectEvent(2, mac));
                    return;
                default:
                    return;
            }
        }

        @Override // com.gzgamut.smart_movement.ble_controller.BleConnectCallBack
        public void onDescriptorWrite(int status) {
            if (status == 0) {
                try {
                    BLEService2.this.getVersion();
                } catch (NoConnectException e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    BLEService2.this.setCharactoristicNotifyAndWriteDescriptor();
                } catch (NoConnectException e2) {
                    e2.printStackTrace();
                }
            }
        }

        @Override // com.gzgamut.smart_movement.ble_controller.BleConnectCallBack
        public void onBluetoothServiceDiscover(boolean status) {
            if (status) {
                try {
                    BLEService2.this.setCharactoristicNotifyAndWriteDescriptor();
                } catch (NoConnectException e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    BLEService2.this.findGattService();
                } catch (NoConnectException e2) {
                    e2.printStackTrace();
                }
            }
        }

        @Override // com.gzgamut.smart_movement.ble_controller.BleConnectCallBack
        public void onSelfDeviceResponse(byte[] value, String mac) {
            switch (ParserHelper.getByteValue(value[0])) {
                case Global.title_receive_current_hour_activity:
                    //更新时间，今日步数
                    BLEService2.this.actionReceiveCurrentHourActivity(value);
                    return;
                case Global.title_receive_set_screen:
                    BLEService2.this.receive_screen(ParserHelper.getByteValue(value[2]));
                    return;
                case Global.title_receive_press_key:
                    BLEService2.this.receive_press_key();
                    return;
                //19.活动强度(基于XYZ三轴加速度的算法获得值，亲情互动自定义)
                case Global.title_receive_xyz:
                    EventBus.getDefault().post(new XYZEvent(BLEService2.this.paraseXYZ(value)));
                    break;
                //心率数据
                case Global.title_receive_heart:
                    if (ParserHelper.getByteValue(value[1]) != 4) {
                        BLEService2.this.receive_heart_status(value);
                        return;
                    }
                    break;
                //血压数据
                case Global.title_receive_blood_data:
                    BLEService2.this.receive_blood_data(value);
                    return;
                case 222:
                    //血压值,收缩压、舒张压
                    BLEService2.this.receive_rri(ParserHelper.getByteValue(value[1]), ParserHelper.getByteValue(value[2]));
                    break;
                case 240:
                    //说明数据同步中
                    BLEService2.this.actionReceiveVersion(value, mac);
                    return;
                case Global.title_receive_clear:
                    EventBus.getDefault().post(new AboutEvent(1));
                    break;
                case Global.title_receive_reset:
                    EventBus.getDefault().post(new AboutEvent(0));
                    break;
            }
            if (BLEService2.this.syncCompat != null) {
                try {
                    BLEService2.this.syncCompat.getSyncData(value);
                } catch (NoConnectException e) {
                    e.printStackTrace();
                }
            }
        }

    };
    private int connectState = 2;
    private BleConnectController controller;
    private String currentTopic;
    private int disConnectedCount = 0;
    private final String hrvUri = "tcp://safe.ceping525.com:1883";
    private boolean isHRVTest = false;
    private final IBinder mBinder = new LocalBinder();
    private MqttCallback mqttCallback = new MqttCallbackExtended() {
        /* class com.gzgamut.smart_movement.service.BLEService2.AnonymousClass3 */

        @Override // org.eclipse.paho.client.mqttv3.MqttCallbackExtended
        public void connectComplete(boolean reconnect, String serverURI) {
            BLEService2.this.mqttConnectState = 55;
        }

        @Override // org.eclipse.paho.client.mqttv3.MqttCallback
        public void connectionLost(Throwable cause) {
            EventBus.getDefault().post(new MqttEvent(2));
        }

        @Override // org.eclipse.paho.client.mqttv3.MqttCallback
        public void messageArrived(String topic, MqttMessage message) throws Exception {
            if (topic.equals(BLEService2.this.currentTopic)) {
                JSONObject mes = new JSONObject(message.toString());
                long curTime = mes.getLong(BLEService2.KEYCURTime);
                EventBus.getDefault().post(new MqttTopicEvent(mes.getDouble(BLEService2.KEYHRV), mes.getInt(BLEService2.KEYHR_COUNT), curTime, mes.getInt(BLEService2.KEYHR_T), mes.getInt(BLEService2.KEYHR_t)));
            }
        }

        @Override // org.eclipse.paho.client.mqttv3.MqttCallback
        public void deliveryComplete(IMqttDeliveryToken token) {
            try {
                switch (token.getMessage().getId()) {
                    case 2:
                        EventBus.getDefault().post(new MqttEvent(6));
                        return;
                    case 3:
                        EventBus.getDefault().post(new MqttEvent(8));
                        return;
                    default:
                        return;
                }
            } catch (MqttException e) {
                e.printStackTrace();
            }
        }
    };

    private int mqttConnectState = 66;
    private final int mqttConnected = 55;
    private final int mqttConnecting = 77;
    private final int mqttDisconnected = 66;
    private BroadcastReceiver notifiReceiver = new BroadcastReceiver() {
        /* class com.gzgamut.smart_movement.service.BLEService2.AnonymousClass5 */

        public void onReceive(Context context, Intent intent) {
            if (BLEService2.this.syncCompat != null) {
                final String packageName = "";// intent.getStringExtra(NotificationAccessbilityService.FROM_WHICH_APPLICATION);
                final String content = "";// intent.getStringExtra(NotificationAccessbilityService.FROM_APPLICATION_CONTENT);
                new Thread(new Runnable() {
                    /* class com.gzgamut.smart_movement.service.BLEService2.AnonymousClass5.AnonymousClass1 */

                    public void run() {
                        String str = packageName;
                        char c = 65535;
                        switch (str.hashCode()) {

                            case -973170826:
                                if (str.equals("com.tencent.mm")) {
                                    c = 1;
                                    break;
                                }
                                break;

                            case 361910168:
                                if (str.equals("com.tencent.mobileqq")) {
                                    c = 0;
                                    break;
                                }
                                break;

                        }
                        switch (c) {
                            case 0:
                                if (DeviceHelper.getWhichNotiChoose(SpHelper.AS_IS_CHOOSE_QQ_NOTY).booleanValue()) {
                                    try {
                                        BLEService2.this.syncCompat.allRemaind(content, 0);
                                        return;
                                    } catch (NoConnectException e) {
                                        e.printStackTrace();
                                        return;
                                    }
                                } else {
                                    return;
                                }
                            case 1:
                                if (DeviceHelper.getWhichNotiChoose(SpHelper.AS_IS_CHOOSE_WECHAT_NOTY).booleanValue()) {
                                    //   BLEService2.this.syncCompat.allRemaind(content, 1);
                                    return;
                                } else {
                                    return;
                                }
                            case 2:
                                if (DeviceHelper.getWhichNotiChoose(SpHelper.AS_IS_CHOOSE_FACEBOOK_NOTY).booleanValue()) {
                                    //   BLEService2.this.syncCompat.allRemaind(content, 4);
                                    return;
                                } else {
                                    return;
                                }
                            case 3:
                                if (DeviceHelper.getWhichNotiChoose(SpHelper.AS_IS_CHOOSE_TWITTER_NOTY).booleanValue()) {
                                    try {
                                        BLEService2.this.syncCompat.allRemaind(content, 5);
                                        return;
                                    } catch (NoConnectException e4) {
                                        e4.printStackTrace();
                                        return;
                                    }
                                } else {
                                    return;
                                }
                            case 4:
                                if (DeviceHelper.getWhichNotiChoose(SpHelper.AS_IS_CHOOSE_GMAIL_NOTY).booleanValue()) {
                                    //   BLEService2.this.syncCompat.allRemaind(content, 6);
                                    return;
                                } else {
                                    return;
                                }
                            case 5:
                                if (DeviceHelper.getWhichNotiChoose(SpHelper.AS_IS_CHOOSE_WHATSUP_NOTY).booleanValue()) {
                                    //  BLEService2.this.syncCompat.allRemaind(content, 7);
                                    return;
                                } else {
                                    return;
                                }
                            case 6:
                                if (DeviceHelper.getWhichNotiChoose(SpHelper.AS_IS_CHOOSE_LINE_NOTY).booleanValue()) {
                                    //  BLEService2.this.syncCompat.allRemaind(content, 8);
                                    return;
                                } else {
                                    return;
                                }
                            case 7:
                                if (DeviceHelper.getWhichNotiChoose(SpHelper.AS_IS_CHOOSE_SKYPE_NOTY).booleanValue()) {
                                    try {
                                        BLEService2.this.syncCompat.allRemaind(content, 9);
                                        return;
                                    } catch (NoConnectException e8) {
                                        e8.printStackTrace();
                                        return;
                                    }
                                } else {
                                    return;
                                }
                            case '\b':
                                if (DeviceHelper.getWhichNotiChoose(SpHelper.AS_IS_CHOOSE_MESSENGER_NOTY).booleanValue()) {
                                    //   BLEService2.this.syncCompat.allRemaind(content, 10);
                                    return;
                                } else {
                                    return;
                                }
                            case '\t':
                                if (DeviceHelper.getWhichNotiChoose(SpHelper.AS_IS_CHOOSE_LINKEDIN_NOTY).booleanValue()) {
                                    //     BLEService2.this.syncCompat.allRemaind(content, 11);
                                    return;
                                } else {
                                    return;
                                }
                            case '\n':
                                if (DeviceHelper.getWhichNotiChoose(SpHelper.AS_IS_CHOOSE_INSTAGRAM_NOTY).booleanValue()) {
                                    //     BLEService2.this.syncCompat.allRemaind(content, 12);
                                    return;
                                } else {
                                    return;
                                }
                            case 11:
                                if (DeviceHelper.getWhichNotiChoose(SpHelper.AS_IS_CHOOSE_VIBER_NOTY).booleanValue()) {
                                    //    BLEService2.this.syncCompat.allRemaind(content, 13);
                                    return;
                                } else {
                                    return;
                                }
                            case '\f':
                                if (DeviceHelper.getWhichNotiChoose(SpHelper.AS_IS_CHOOSE_KAKAOTALK_NOTY).booleanValue()) {
                                    //     BLEService2.this.syncCompat.allRemaind(content, 14);
                                    return;
                                } else {
                                    return;
                                }
                            case '\r':
                                if (DeviceHelper.getWhichNotiChoose(SpHelper.AS_IS_CHOOSE_VKONTAKTE_NOTY).booleanValue()) {
                                    //    BLEService2.this.syncCompat.allRemaind(content, 15);
                                    return;
                                } else {
                                    return;
                                }
                            default:
                                return;
                        }
                    }
                }).start();
            }
        }
    };
    private SyncCompat.onSyncStateChangeListener onSyncStateChangeListener = new SyncCompat.onSyncStateChangeListener() {
        /* class com.gzgamut.smart_movement.service.BLEService2.AnonymousClass7 */

        @Override // com.gzgamut.smart_movement.synccontroller.SyncCompat.onSyncStateChangeListener
        public void onSyncOver() {
            EventBus.getDefault().post(new DeviceSyncEvent(1));
        }

        @Override // com.gzgamut.smart_movement.synccontroller.SyncCompat.onSyncStateChangeListener
        public void onSyncFinish() {
            EventBus.getDefault().post(new DeviceSyncEvent(0));
        }

        @Override // com.gzgamut.smart_movement.synccontroller.SyncCompat.onSyncStateChangeListener
        public void onSyncFail() {
            EventBus.getDefault().post(new DeviceSyncEvent(2));
        }

        @Override // com.gzgamut.smart_movement.synccontroller.SyncCompat.onSyncStateChangeListener
        public void onBatteryReceived() {
            EventBus.getDefault().post(new DeviceSyncEvent(4));
        }

        @Override // com.gzgamut.smart_movement.synccontroller.SyncCompat.onSyncStateChangeListener
        public void onSingleSyncFail() {
            EventBus.getDefault().post(new DeviceSyncEvent(3));
        }
    };
    private final String passWord = "Xsjz56789";
    private SyncCompat syncCompat = null;
    private final String userName = "bnush";

    public class LocalBinder extends Binder {
        public LocalBinder() {
        }

        public BLEService2 getService() {
            return BLEService2.this;
        }
    }

    @Nullable
    public IBinder onBind(Intent intent) {
        return this.mBinder;
    }

    public void onCreate() {
        init();
        initBroadCast();
        if (Build.VERSION.SDK_INT >= 26) {
            forceGroundStartNewApi();
        } else {
            forceGroundStart();
        }
    }


    public int getConnectState() {
        return this.connectState;
    }

    public void onDestroy() {
        super.onDestroy();

        this.controller = null;
        disConnectMqtt();
        stopForeground(true);
        LocalBroadcastManager.getInstance(this).unregisterReceiver(this.notifiReceiver);
    }

    private void init() {
        this.controller = new BleConnectController(getApplicationContext());
        this.controller.registCallback(this.connectCallBack);
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private String paraseXYZ(byte[] value) {
        String hex = Integer.toHexString(value[3] & 255);
        if (hex.length() == 1) {
            hex = '0' + hex;
        }
        String hex2 = Integer.toHexString(value[4] & 255);
        if (hex2.length() == 1) {
            hex2 = '0' + hex2;
        }
        String hey = Integer.toHexString(value[5] & 255);
        if (hey.length() == 1) {
            hey = '0' + hey;
        }
        String hey2 = Integer.toHexString(value[6] & 255);
        if (hey2.length() == 1) {
            hey2 = '0' + hey2;
        }
        String hez = Integer.toHexString(value[7] & 255);
        if (hez.length() == 1) {
            hez = '0' + hez;
        }
        String hez2 = Integer.toHexString(value[8] & 255);
        if (hez2.length() == 1) {
            hez2 = '0' + hez2;
        }
        return hex + hex2 + hey + hey2 + hez + hez2;
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void receive_rri(int rriLow, int rriHigh) {
        if (rriLow < 0) {
            rriLow += 256;
        }
        if (rriHigh < 0) {
            rriHigh += 256;
        }
        String low = Integer.toHexString(rriLow);
        String high = Integer.toHexString(rriHigh);
        int lowLen = low.length();
        int highLen = high.length();
        if (lowLen > 2) {
            low = low.substring(lowLen - 2, lowLen);
        } else if (lowLen < 2) {
            low = "0" + low;
        }
        if (highLen > 2) {
            high = high.substring(highLen - 2, highLen);
        } else if (highLen < 2) {
            high = "0" + high;
        }
        EventBus.getDefault().post(new RRiReceiveEvent(low, high));
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void receive_screen(int third) {
        EventBus.getDefault().post(new DeviceScreenEvent(third));
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void receive_heart_status(byte[] value) {
        EventBus.getDefault().post(new HeartStatusEvent(value));
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void receive_press_key() {
        EventBus.getDefault().post(new DeviceButtonPushEvent());
    }

    //http://47.110.135.34:18083/#/connections
    //http://10.1.120.98:18083/  admin lianxin@2020
    //http://112.126.65.143:18083/
    //nc -v 127.0.0.1 11000
    //nc -v safe.ceping525.com 1883
    private MqttConnectOptions options;

    public void connectMqttForHrv() {
        if (this.mqttConnectState == 66) {
            if (this.options == null) {
                this.options = new MqttConnectOptions();
                this.options.setCleanSession(true);
                this.options.setConnectionTimeout(5);
                this.options.setKeepAliveInterval(60);
                this.options.setUserName("admin");
                this.options.setPassword("zz123456".toCharArray());
            }
            if (this.client == null) {
                this.client = new MqttAndroidClient(getApplicationContext(), "tcp://112.126.65.143:1883", Settings.System.getString(getContentResolver(), "android_id"));
                this.client.setCallback(this.mqttCallback);
            }
            this.mqttConnectState = 77;
            try {
                this.client.connect(this.options, null, new IMqttActionListener() {
                    /* class com.gzgamut.smart_movement.service.BLEService2.AnonymousClass1 */

                    @Override // org.eclipse.paho.client.mqttv3.IMqttActionListener
                    public void onSuccess(IMqttToken asyncActionToken) {
                        BLEService2.this.mqttConnectState = 55;
                        EventBus.getDefault().post(new MqttEvent(1));
                    }

                    @Override // org.eclipse.paho.client.mqttv3.IMqttActionListener
                    public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                        BLEService2.this.mqttConnectState = 66;
                        EventBus.getDefault().post(new MqttEvent(3));
                    }
                });
            } catch (MqttException e) {
                e.printStackTrace();
            }
        } else if (this.mqttConnectState == 55) {
            EventBus.getDefault().post(new MqttEvent(1));
        }
    }

    //订阅
    public void subscribeTopic(final String topic) {
        try {
            this.client.subscribe(topic, 2, (Object) null, new IMqttActionListener() {
                /* class com.gzgamut.smart_movement.service.BLEService2.AnonymousClass2 */

                @Override // org.eclipse.paho.client.mqttv3.IMqttActionListener
                public void onSuccess(IMqttToken asyncActionToken) {
                    BLEService2.this.currentTopic = topic;
                    EventBus.getDefault().post(new MqttEvent(4));
                }

                @Override // org.eclipse.paho.client.mqttv3.IMqttActionListener
                public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                    EventBus.getDefault().post(new MqttEvent(5));
                }
            });
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }

    public void unSubscribeTopic(String topic) {
        try {
            if (this.client != null && this.client.isConnected()) {
                this.client.unsubscribe(topic);
            }
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }

    public void publishMessge(MqttMessage message) {
        try {
            this.client.publish("bh-00/heartrate/ch1", message, (Object) null, new IMqttActionListener() {
                /* class com.gzgamut.smart_movement.service.BLEService2.AnonymousClass4 */

                @Override // org.eclipse.paho.client.mqttv3.IMqttActionListener
                public void onSuccess(IMqttToken asyncActionToken) {
                }

                @Override // org.eclipse.paho.client.mqttv3.IMqttActionListener
                public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                    EventBus.getDefault().post(new MqttEvent(7));
                }
            });
        } catch (MqttPersistenceException e) {
            e.printStackTrace();
        } catch (MqttException e2) {
            e2.printStackTrace();
        }
    }

    public void disConnectMqtt() {
        if (this.client != null && this.client.isConnected()) {
            try {
                this.client.disconnect();
                this.client.close();
            } catch (MqttException e) {
                e.printStackTrace();
            }
        }
    }

    private void initBroadCast() {
        IntentFilter notifiIntent = new IntentFilter();
        notifiIntent.addAction(NotificationAccessbilityService.NOTIFYCATION_ACTION);
        LocalBroadcastManager.getInstance(this).registerReceiver(this.notifiReceiver, notifiIntent);
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void actionReceiveVersion(byte[] value, String mac) {
        String version = "";//SyncResultHelper.parseVersion(value);
        VersionHelper.setVersionWatch(version);
        String type = VersionHelper.parserImgVersion(version).getType();
        this.connectState = 0;
        if (DeviceHelper.isT(DeviceHelper.getNewSaveDeviceName())) {
            this.syncCompat = SyncCompat.getSyncCompatImp(15, this.controller);
            this.syncCompat.setOnSyncStateChangeListener(this.onSyncStateChangeListener);
            EventBus.getDefault().post(new DeviceConnectEvent(0, mac));
            return;
        }
        if (DeviceHelper.isS(DeviceHelper.getNewSaveDeviceName())) {
            if (DeviceHelper.isS1_9(DeviceHelper.getNewSaveDeviceName()) || DeviceHelper.isSA_R(DeviceHelper.getNewSaveDeviceName()) || DeviceHelper.isST_Z(DeviceHelper.getNewSaveDeviceName())) {
                this.syncCompat = SyncCompat.getSyncCompatImp(16, this.controller);
                this.syncCompat.setOnSyncStateChangeListener(this.onSyncStateChangeListener);
                EventBus.getDefault().post(new DeviceConnectEvent(0, mac));
                return;
            }
        } else if (DeviceHelper.isH(DeviceHelper.getNewSaveDeviceName())) {
            this.syncCompat = SyncCompat.getSyncCompatImp(17, this.controller);
            this.syncCompat.setOnSyncStateChangeListener(this.onSyncStateChangeListener);
            //EventBus.getDefault().post(new DeviceConnectEvent(0, mac));
            return;
        }
        char c = 65535;
        switch (type.hashCode()) {
            case 2314:
                if (type.equals("I3")) {
                    c = 0;
                    break;
                }
                break;
            case 70731:
                if (type.equals(VersionHelper.TYPE_VERSION_H03)) {
                    c = 7;
                    break;
                }
                break;
            case 71720:
                if (type.equals("I10")) {
                    c = 1;
                    break;
                }
                break;
            case 71901:
                if (type.equals("I7+")) {
                    c = 2;
                    break;
                }
                break;
            case 71941:
                if (type.equals(DeviceHelper.DEVICE_I7S)) {
                    c = 6;
                    break;
                }
                break;
            case 2543387:
                if (type.equals("SH06")) {
                    c = 3;
                    break;
                }
                break;
            case 2543390:
                if (type.equals("SH09")) {
                    c = 4;
                    break;
                }
                break;
            case 78845175:
                if (type.equals("SH09U")) {
                    c = 5;
                    break;
                }
                break;
        }
        switch (c) {
            case 0:
                this.syncCompat = SyncCompat.getSyncCompatImp(10, this.controller);
                this.syncCompat.setOnSyncStateChangeListener(this.onSyncStateChangeListener);
                //   EventBus.getDefault().post(new DeviceConnectEvent(0, mac));
                return;
            case 1:
                this.syncCompat = SyncCompat.getSyncCompatImp(9, this.controller);
                this.syncCompat.setOnSyncStateChangeListener(this.onSyncStateChangeListener);
                //  EventBus.getDefault().post(new DeviceConnectEvent(0, mac));
                return;
            case 2:
                this.syncCompat = SyncCompat.getSyncCompatImp(8, this.controller);
                this.syncCompat.setOnSyncStateChangeListener(this.onSyncStateChangeListener);
                //    EventBus.getDefault().post(new DeviceConnectEvent(0, mac));
                return;
            case 3:
                this.syncCompat = SyncCompat.getSyncCompatImp(1, this.controller);
                this.syncCompat.setOnSyncStateChangeListener(this.onSyncStateChangeListener);
                // EventBus.getDefault().post(new DeviceConnectEvent(0, mac));
                return;
            case 4:
                this.syncCompat = SyncCompat.getSyncCompatImp(3, this.controller);
                this.syncCompat.setOnSyncStateChangeListener(this.onSyncStateChangeListener);
                //   EventBus.getDefault().post(new DeviceConnectEvent(0, mac));
                return;
            case 5:
                if (DeviceHelper.getDeviceName().equalsIgnoreCase("SH09U") || DeviceHelper.getDeviceName().equalsIgnoreCase(DeviceHelper.DEVICE_SH09u)) {
                    this.syncCompat = SyncCompat.getSyncCompatImp(5, this.controller);
                    this.syncCompat.setOnSyncStateChangeListener(this.onSyncStateChangeListener);
                    //    EventBus.getDefault().post(new DeviceConnectEvent(0, mac));
                    return;
                } else if (DeviceHelper.getDeviceName().equalsIgnoreCase(DeviceHelper.DEVICE_SS)) {
                    this.syncCompat = SyncCompat.getSyncCompatImp(13, this.controller);
                    this.syncCompat.setOnSyncStateChangeListener(this.onSyncStateChangeListener);
                    // EventBus.getDefault().post(new DeviceConnectEvent(0, mac));
                    return;
                } else {
                    return;
                }
            case 6:
                this.syncCompat = SyncCompat.getSyncCompatImp(11, this.controller);
                this.syncCompat.setOnSyncStateChangeListener(this.onSyncStateChangeListener);
                //    EventBus.getDefault().post(new DeviceConnectEvent(0, mac));
                return;
            case 7:
                this.syncCompat = SyncCompat.getSyncCompatImp(12, this.controller);
                this.syncCompat.setOnSyncStateChangeListener(this.onSyncStateChangeListener);
                //    EventBus.getDefault().post(new DeviceConnectEvent(0, mac));
                return;
            default:
                return;
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void actionReceiveCurrentHourActivity(byte[] value) {
        int profileID = 0;// DatabaseProvider.getProfileID(getApplicationContext());
        List<HistoryHour> historyHours = null; //SyncResultHelper.parseCurrentHourActivity(getApplicationContext(), value);
        if (historyHours.get(0) != null) {
            HistoryHour now = historyHours.get(0);
            Calendar search_cal = Calendar.getInstance();
            search_cal.setTimeInMillis(now.getDate().getTimeInMillis());
            HistoryHour old = null;// DatabaseProvider.getInstance(getApplicationContext()).queryHistoryHour(getApplicationContext(), profileID, search_cal);
            //   DatabaseProvider.getInstance(getApplicationContext()).saveAutoHistoryHour(getApplicationContext(), historyHours, profileID, 5);
            //    DealOldDataHelper.getStepDataByDateAndSaveNewDaily(getApplicationContext(), DeviceHelper.getDefaultDevice(), profileID, search_cal);
            if (old != null) {
                EventBus.getDefault().post(new StepUpdateEvent(now.getStep() - old.getStep(), now.getDate()));
            }
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void receive_blood_data(byte[] value) {
        int profileID = MainActivity.initProfileID(getApplicationContext());
        Calendar calendar = Calendar.getInstance();
        if (value.length >= 3) {
            int sbp = 0;// ParserHelper.getByteValue(value[1]);
            int dbp = 0;//ParserHelper.getByteValue(value[2]);
            String date = FormatHelper.sdf_dd_MM_yyyy.format(calendar.getTime());
            // DatabaseProvider.getInstance(getApplicationContext()).saveBloodDate(getApplicationContext(), date, profileID);
            //DatabaseProvider.getInstance(getApplicationContext()).saveBloodData(getApplicationContext(), date, FormatHelper.sdf_HH_mm.format(calendar.getTime()), sbp + MqttTopic.TOPIC_LEVEL_SEPARATOR + dbp + "mmhg", profileID);
            EventBus.getDefault().post(new BloodEvent());
        }
    }

    public void connect(String mac) {
        this.connectState = 4;
        this.controller.connect(mac);
    }

    public void disConnect() {
        this.controller.disconnect();
    }

    public void findGattService() throws NoConnectException {
        this.controller.findGattService();
    }

    public void setCharactoristicNotifyAndWriteDescriptor() throws NoConnectException {
        this.controller.self_notifyAndWriteDescriptor(Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_NOTI, Global.UUID_DESCRIPTOR_CONFIGURATION);
    }

    public void getVersion() throws NoConnectException {
        this.controller.self_writeData(Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, CommandHelper.getVersionValue());
    }

    public void sync() {
        try {
            this.syncCompat.startSync();
        } catch (NoConnectException e) {
            e.printStackTrace();
        }
    }

    public void setCallRemaind(String number) throws NoConnectException {
        if (this.connectState == 0) {
            this.syncCompat.setCallRemaind(number);
        }
    }

    public void setSmsRemaind(String number, String messageBody) throws NoConnectException {
        if (this.connectState == 0) {
            this.syncCompat.setSmsRemaind(number, messageBody);
        }
    }

    public boolean getIsSync() {
        return this.syncCompat.isSyncing();
    }

    public void setIsHRVTest(boolean HRVStatus) {
        this.isHRVTest = HRVStatus;
    }

    public boolean isHRVTest() {
        return this.isHRVTest;
    }

    //心率数据
    public void set_heart(byte type) throws NoConnectException {
        this.controller.self_writeData(Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, CommandHelper.getHeartValue(type));
    }

    //获取HRV/RRI
    public void getRRi() throws NoConnectException {
        this.controller.self_writeData(Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, CommandHelper.getRRiValue());
    }

    public void setRRi(int T, int t) throws NoConnectException {
        this.controller.self_writeData(Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, CommandHelper.setRRiValue(T, t));
    }

    //点量
    public void getBattery() throws NoConnectException {
        this.controller.self_writeData(Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, CommandHelper.getBatteryValue());
    }

    public void writeData(byte[] value) throws NoConnectException {
        this.controller.self_writeData(Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, value);
    }

    public void forceGroundStart() {
        Notification.Builder builder = new Notification.Builder(getApplicationContext());
        new Intent(this, MyApp.class);
        builder.setContentTitle(getString(R.string.Force_Service_Noty_Title)).setSmallIcon(R.mipmap.image_app_logo_smmm).setWhen(System.currentTimeMillis());
        startForeground(1, builder.build());
    }

    @TargetApi(26)
    public void forceGroundStartNewApi() {
        NotificationChannel channel = new NotificationChannel("PUSH_NOTIFY_ID", "PUSH_NOTIFY_NAME", NotificationManager.IMPORTANCE_LOW);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), "PUSH_NOTIFY_ID");
        builder.setSmallIcon(R.mipmap.image_app_logo_smmm).setContentTitle(getString(R.string.Force_Service_Noty_Title)).setAutoCancel(true).setOngoing(true);
        ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
        startForeground(1, builder.build());
    }
}
